<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务状态" prop="jobConfigId">
        <el-input
          v-model="queryParams.jobConfigId"
          placeholder="请输入任务状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="jobName">
        <el-input
          v-model="queryParams.jobName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>



      <el-form-item label="提交模式" prop="deployMode">
              <el-select v-model="form.deployMode" placeholder="请选择提交模式">
                <el-option
                  v-for="dict in deployModeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
      
      <el-form-item label="启动时间" prop="startTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择启动时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="关闭时间" prop="endTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择启动时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="任务状态" prop="jobStatus">
        <el-select v-model="queryParams.jobStatus" placeholder="请选择任务状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      
      
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['streaming:JobRunLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['streaming:JobRunLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="JobRunLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="Job任务Id" align="center" prop="jobConfigId" />
      <el-table-column label="任务名称" align="center" prop="jobName" />
      <el-table-column label="提交模式" align="center" prop="deployMode" :formatter="deployModeFormat" />
      <el-table-column label="运行后的任务id" align="center" prop="jobId" />
      <!-- <el-table-column label="启动时本地日志" align="center" prop="localLog" /> -->
      <el-table-column label="远程日志url的地址" align="center" prop="remoteLogUrl" />
      <el-table-column label="启动时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关闭时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="jobStatus"  />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['streaming:JobRunLog:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['streaming:JobRunLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运行任务日志对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="1000px" append-to-body>
      <el-form ref="form" :model="form"  label-width="150px">
        <el-row>
            <el-col :span="12">
              <el-form-item label="Job任务Id" prop="jobConfigId">
                <el-input v-model="form.jobConfigId" :disabled="true" />
              </el-form-item>
              <el-form-item label="任务名称" prop="jobName">
                <el-input v-model="form.jobName" :disabled="true" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交模式" prop="deployMode">
                <el-input v-model="form.deployMode" :disabled="true" />
              </el-form-item>
              <el-form-item label="运行后的任务id" prop="jobId">
                <el-input v-model="form.jobId" :disabled="true" />
              </el-form-item> 
            </el-col>

            <el-form-item label="启动时本地日志：" prop="localLog">
              <el-input v-model="form.localLog" type="textarea" autosize="true" :disabled="true" />
            </el-form-item>
            <el-form-item label="远程日志url的地址：" prop="localLog">
              <el-input v-model="form.remoteLogUrl" type="textarea" :disabled="true" />
            </el-form-item>

            <el-col :span="12">
              <el-form-item label="启动时间" prop="startTime">
                <el-date-picker clearable size="small"
                  v-model="form.startTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择启动时间"
                  :disabled="true">
                </el-date-picker>
              </el-form-item>
              
            </el-col>
            <el-col :span="12">
              <el-form-item label="关闭时间" prop="endTime">
                <el-date-picker clearable size="small"
                  v-model="form.endTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择启动时间"
                  :disabled="true">
                </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>  
    </el-dialog>
  </div>
</template>

<script>
import { listJobRunLog, getJobRunLog, delJobRunLog, exportJobRunLog } from "@/api/streaming/JobRunLog";

export default {
  name: "JobRunLog",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运行任务日志表格数据
      JobRunLogList: [],
      // 弹出层标题
      title: "",
       // 提交模式字典
      deployModeOptions: [],
      // 任务状态字典
      statusOptions: [],
      // 是否显示详细弹出层
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobConfigId: null,
        jobName: null,
        deployMode: null,
        jobId: null,
        localLog: null,
        remoteLogUrl: null,
        startTime: null,
        endTime: null,
        jobStatus: null,
        isDeleted: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jobConfigId: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        jobName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        deployMode: [
          { required: true, message: "提交模式: standalone 、yarn 、yarn-session 不能为空", trigger: "blur" }
        ],
        isDeleted: [
          { required: true, message: "提交模式: standalone 、yarn 、yarn-session 不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("flink_config_model").then(response => {
      this.deployModeOptions = response.data;
    });
    this.getDicts("flink_config_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询运行任务日志列表 */
    getList() {
      this.loading = true;
      listJobRunLog(this.queryParams).then(response => {
        this.JobRunLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 提交模式字典翻译
    deployModeFormat(row, column) {
      return this.selectDictLabel(this.deployModeOptions, row.deployMode);
    },
    
    // 任务状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jobConfigId: null,
        jobName: null,
        deployMode: null,
        jobId: null,
        localLog: null,
        remoteLogUrl: null,
        startTime: null,
        endTime: null,
        jobStatus: "0",
        isDeleted: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    
    /** 修改按钮操作 */
    handleView(row) {
      const id = row.id || this.ids
      getJobRunLog(id).then(response => {
       
        this.form = response.data;
        this.openView = true;  
      });
    },
   
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除运行任务日志编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delJobRunLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有运行任务日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportJobRunLog(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    
  }
};
</script>
